import request from "./request"

// 获取订单列表
export const getOrders = async (page: number = 1, pageSize: number = 10) => {
    try {
        const data = await request.get(`/api/orders?page=${page}&pageSize=${pageSize}`)
        return { 
            data,
            message: '获取数据成功',
            code: 200
        }
    } catch (error: any) {
        console.error('获取订单数据失败:', error)
        return { 
            data: {
                list: [],
                pagination: {
                    page: 1,
                    pageSize: 10,
                    total: 0,
                    totalPage: 0
                }
            },
            message: error.message || '获取数据失败',
            code: 500
        }
    }
}

// 获取订单详情
export const getOrderById = async (id: number) => {
    try {
        const data = await request.get(`/api/orders/${id}`)
        return { 
            data,
            message: '获取数据成功',
            code: 200
        }
    } catch (error: any) {
        console.error('获取订单详情失败:', error)
        return { 
            data: null,
            message: error.message || '获取数据失败',
            code: 500
        }
    }
}

// 创建订单
export const createOrder = async (orderData: any) => {
    try {
        const data = await request.post('/api/orders', orderData)
        return { 
            data,
            message: '创建订单成功',
            code: 200
        }
    } catch (error: any) {
        console.error('创建订单失败:', error)
        return { 
            data: null,
            message: error.message || '创建订单失败',
            code: 500
        }
    }
}

// 更新订单
export const updateOrder = async (id: number, orderData: any) => {
    try {
        const data = await request.put(`/api/orders/${id}`, orderData)
        return { 
            data,
            message: '更新订单成功',
            code: 200
        }
    } catch (error: any) {
        console.error('更新订单失败:', error)
        return { 
            data: null,
            message: error.message || '更新订单失败',
            code: 500
        }
    }
}

// 删除订单
export const deleteOrder = async (id: number) => {
    try {
        const data = await request.delete(`/api/orders/${id}`)
        return { 
            data,
            message: '删除订单成功',
            code: 200
        }
    } catch (error: any) {
        console.error('删除订单失败:', error)
        return { 
            data: null,
            message: error.message || '删除订单失败',
            code: 500
        }
    }
}